<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id.toString()">
        <!-- 左侧头像区域 -->
        <template #icon>
          <van-image
            round
            width="30"
            height="30"
            style="margin-right: 10px"
            :src="item.aut_photo"
          />
        </template>
        <!-- 评论标题区域 -->
        <template #title>
          <span style="color: #466b9d">{{ item.aut_name }}</span>
        </template>
        <!-- 评论详情区域 -->
        <template #label>
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{
              _formatTime(item.pubdate)
            }}</span>
          </p>
        </template>
        <!--右侧侧点赞评论区域 -->
        <template #right-icon>
          <van-icon :name="item.is_liking?'like':'like-o'" @click="likeComment(item)" />
        </template>
      </van-cell>
    </van-list>

    <!-- 发布评论区域 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="commentContent"
      >
        <template #button>
          <van-button size="mini" @click="addComments">发布</van-button>
        </template>
      </van-field>
    </van-cell-group>
  </div>
</template>

<script>
import { getComments, addComment, addCommentLike, deleteCommentLike } from '@/api/comment'
export default {
  name: 'ArticleComment',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      commentContent: null
    }
  },
  methods: {
    async likeComment (item) {
      console.log(item)
      if (item.is_liking) {
        await deleteCommentLike(item.com_id.toString())
        this.$toast.success('取消点赞成功')
        // console.log(a)
      } else {
        await addCommentLike(item.com_id.toString())
        // console.log(b)
        this.$toast.success('点赞成功')
      }
      // 修改数据
      item.is_liking = !item.is_liking
    },
    async addComments () {
      if (this.commentContent === '') {
        this.$toast.success('请输入内容')
        return false
      }
      const res = await addComment(this.$route.query.id, this.commentContent)
      console.log(res)
      this.list.unshift(res.data.data.new_obj)
      this.commentContent = ''
      this.$toast.success('表布评论成功')
    },
    async onLoad () {
      // 异步更新数据
    //   console.log('评论数据加载')
      const res = await getComments(this.$route.query.id, this.offset)
      console.log(res)
      const newArr = res.data.data.results
      this.list.push(...newArr)
      this.loading = false
      if (newArr.length === 0) {
        this.finished = true
      }
      this.offset = res.data.data.last_id
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
